/*
 * Copyright 2007. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the GNU Lesser General Public License, Version 2. You
 * can find a copy of the license at:
 * 
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */
package com.sinai.mshab.client.ui.basic;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

/**
 * This makes a new dialog box pop up in the center of the user's screen
 * with the message you provide.
 * @author serge
 * 
 */
public class Warning extends DialogBox {

	/**
	 * Creates a new warning pop up.
	 * 
	 * @param message
	 *            the message to be displayed on the pop-up.
	 * @param left
	 *            how many pixels from the left this warning should appear
	 * @param right
	 *            how many pixels from the top should this warning appear.
	 */
	public Warning(String message, int left, int top) {
		super(true);
		setText("Warning");

		Image m = new Image("images/close.gif");
		Image warning = new Image("images/warning.gif");
		warning.setStyleName("warning_logo");

		Button closeButton = new Button("Close");
		closeButton.setStyleName("button");
		closeButton.addClickListener(new CloseListener());
		HTML msg = new HTML(message, true);
		msg.setStyleName("warning_text");

		DockPanel dock = new DockPanel();
		dock.setSpacing(4);

		dock.add(closeButton, DockPanel.SOUTH);
		dock.add(warning, DockPanel.WEST);
		dock.add(msg, DockPanel.EAST);

		dock.setCellHorizontalAlignment(closeButton, DockPanel.ALIGN_CENTER);
		dock.setWidth("100%");
		setWidget(dock);

		int left_pos = (Window.getClientWidth() - getOffsetWidth()) / 2
				+ getBodyScrollLeft() - 200;
		int top_pos = (Window.getClientHeight() - getOffsetHeight()) / 2
				+ getBodyScrollTop() - 100;
		setPopupPosition(left_pos, top_pos);

		show();

	}

	private native int getBodyScrollLeft() /*-{
	 return $doc.body.scrollLeft;
	 }-*/;

	private native int getBodyScrollTop() /*-{
	 return $doc.body.scrollTop;
	 }-*/;

	/**
	 * This listener is attached to the button that logins users.
	 */
	private class CloseListener implements ClickListener {
		public void onClick(Widget sender) {
			hide();
		}
	}

	private class MyPositionCallBack implements PositionCallback {
		public void setPosition(int offsetWidth, int offsetHeight) {
			setPopupPosition(offsetWidth + 100, offsetHeight + 100);
		}
	}

}
